<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'look';
cfc('core')->checklogin();
$basic=$this->plugin_setting('basic');
if($_RQ['op']=='look'){
	$resultJson = [];
	$pid = intval($_RQ['pid']);
	$item=pdo_get('jy_aihire_item',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']),array('title','redmoney','mid','rednum','type'));
	$pidcode='red_'.$item['type'].'_'.$pid;
	$isgetred=model('core/red_pack')->check_getred($pidcode,'jy_aihire');
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'pidcode'=>$pidcode,
		'plugin'=>'jy_aihire',
	);
	$psize = 20;
	$pindex = max(1, intval($_RQ['page']));
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>'id desc',
	);
	$redlist=model('core/red_pack')->getall($parameter);
  	$sender = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));  
	$nickname  = $sender['nickname'];
  	$send_name = $send_name?$send_name:$nickname;
  	$send_avatar = $member['logo'] ? $member['logo'] : $sender['avatar'];
  	$send_avatar = $send_avatar ? $send_avatar :  '../core/resource/images/nopic.jpg';
	$parameter['sum']='money';
	$issendred=model('core/red_pack')->getsum($parameter);
	$issendnum=count($redlist);	
	$resultJson['sharedata']=array(
		'title'=> '红包来了',
		'desc'=> $item['title'],
		'link'=>custom_url('jy_aihire/item//id='.$pid.'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=> tomedia('../addons/jy_aihire/resource/images/h-480.png'),
	);
	//获取红包领取记录
	if (!empty($redlist)) {
              	$red_receivers = array_column($redlist, 'mid');
          		$red_receivers = pdo_getall('core_members', array('id' => $red_receivers), array('nickname', 'avatar', 'id'), 'id');
				foreach ($redlist as &$r) {
					$red_receivers[''.$r['mid']]['avatar']=tomedia($red_receivers[''.$r['mid']]['avatar']);
                	$r = array_merge($r, $red_receivers[''.$r['mid']]);
                }
    }
	$get_money=model('core/red_pack')->getone_money($pidcode,'jy_aihire');
    $money = $get_money;
    $resultJson = array_merge($resultJson, [
			'isgetred'=>$isgetred,
            'red_isideal' => 'yes',
            'red_result' => 'success',
            'red_total_amount' => $item['redmoney'],
            'red_given_amount' => $issendred,
            'red_this_amount' => $money,
            'red_total_count' => $item['rednum'],
            'red_given_count' => $issendnum,
            'red_sender_name' => $send_name,
            'red_sender_avatar' => $send_avatar,
            'red_title' => $item['title'],
            'red_given' => $redlist
    ]);
    exi($resultJson);
}
if($_RQ['op']=='getred'){ 
  	$resultJson = [];
	$pid = intval($_RQ['pid']);
	$item=pdo_get('jy_aihire_item',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']),array('redmoney','title','mid','rednum','type'));
	$pidcode='red_'.$item['type'].'_'.$pid;
	$isgetred=model('core/red_pack')->check_getred($pidcode,'jy_aihire');
	if($isgetred){
		exi('您已抢过该红包！','error');
	}
	$params=array(
		'mid'=>$_SESSION['uid'],
		'uniacid'=>$_SESSION['uniacid'],
		'pidcode'=>$pidcode,
		'plugin'=>'jy_aihire',
	);
	$psize = 20;
	$pindex = max(1, intval($_RQ['page']));
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>'id desc',
	);
	$redlist=model('core/red_pack')->getall($parameter);
  	$sender = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));  
	$nickname  = $sender['nickname'];
  	$send_name = $send_name?$send_name:$nickname;
  	$send_avatar = $member['logo'] ? $member['logo'] : $sender['avatar'];
  	$send_avatar = $send_avatar ? $send_avatar :  '../core/resource/images/nopic.jpg';
  	$resultJson['sharedata']=array(
		'title'=> '红包来了',
		'desc'=> $item['title'],
		'link'=>custom_url('jy_aihire/item_detail//id='.$pid.'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=> tomedia('../addons/jy_aihire/resource/images/h-480.png'),
	);
	$parameter['sum']='money';
	$issendred=model('core/red_pack')->getsum($parameter);
	$issendnum=count($redlist);		// 剩余金额
  	$yufee=$item['redmoney']-$issendred;
  	// 剩余次数
	$yutotal=$item['rednum']-$issendnum;
	if($yutotal<1 && $yufee<=0){
		exi('噢！红包已抢完，下次再来吧！','error');
	}
	$params =array(
			'money'=>$yufee,
			'mid'=>$_SESSION['uid'],
			'pidcode'=>$pidcode,
			'plugin'=>'jy_aihire',
			'total_num'=>$yutotal,
			'remark'=>'抢红包-'.$item['title'],
	);
     
	$get_money=cfc('red_pack')->get_money($params,1);
		//$get_money = 0.46666666;
      	if($get_money){
          $get_money = round($get_money, 2);
			//获取红包领取记录
			if (!empty($redlist)) {
					$red_receivers = array_column($redlist, 'mid');
					$red_receivers = pdo_getall('core_members', array('id' => $red_receivers), array('nickname', 'avatar', 'id'), 'id');
					foreach ($redlist as &$r) {
						$r['avatar']=tomedia($r['avatar']);
						$r = array_merge($r, $red_receivers[''.$r['mid']]);
					}
			}
          $money = $get_money;
          $resultJson = array_merge($resultJson, [
            'red_isideal' => 'yes',
            'red_result' => 'success',
            'red_total_amount' => $red['fee'],
            'red_given_amount' => $issendred + $money,
            'red_this_amount' => $money,
            'red_total_count' => $item['rednum'],
            'red_given_count' => $issendnum + 1,
            'red_sender_name' => $send_name,
            'red_sender_avatar' => $send_avatar,
            'red_title' => $item['title'],
            'red_given' => $redlist
            ]);
          exi($resultJson);
		} else{
			exi('红包获取失败','error','goback');
		}
	
}